package com.cw.demo.springbean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import com.alibaba.fastjson.JSON;

import lombok.extern.slf4j.Slf4j;

/**
 * TODO
 *
 * @author chenw
 * @date 2021/1/8
 */
@Slf4j
public class Person implements BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {

    private String name;
    private String address;
    private String phone;

    private BeanFactory beanFactory;
    private String beanName;

    public Person() {
        log.info("【构造器】调用Person的构造器实例化");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        log.info("【注入属性】注入属性name");
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        log.info("【注入属性】注入属性address");
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        log.info("【注入属性】注入属性phone");
        this.phone = phone;
    }

    public BeanFactory getBeanFactory() {
        return beanFactory;
    }

    public String getBeanName() {
        return beanName;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        log.info("【BeanFactoryAware接口】调用BeanFactoryAware接口的setBeanFactory方法:{}", JSON.toJSONString(beanFactory));
    }

    @Override
    public void setBeanName(String s) {
        log.info("【BeanNameAware接口】调用BeanNameAware接口的setBeanName方法：{}",s);
    }

    @Override
    public void destroy() throws Exception {
        log.info("【DisposableBean接口】调用DisposableBean的destory方法");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("【InitializingBean接口】调用InitializingBean的afterPropertiesSet方法");
    }

    public void myInit(){
        log.info("【init-method】调用bean的[init-method]属性指定的初始化方法");
    }

    public void myDestory(){
        log.info("【destory-method】调用bean的[destory-method]属性指定的初始化方法");
    }

}
